<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     @click="handleSendBatch">下 发
          </el-button>
          <el-button type="primary"
                     size="small"
                     @click.stop="handleExport()">导 出
          </el-button>
          <el-button type="primary"
                     size="small"
                     :hidden="true"
                     v-if="false"
                     @click.stop="handlePrint()">打 印
          </el-button>
          <el-upload
            class="upload-inline"
            action=""
            :show-file-list="false"
            accept=".xls,.xlsx"
            :http-request="handleImportDeliveryTemp"
          >
          <el-button type="primary" size="small">导入</el-button>
          </el-upload>
          <el-button
            type="primary"
            size="small"
            @click="delBatchs()"
          >删除
          </el-button>
	  <el-button
            type="primary"
            size="small"
	    v-permission="permissionList.delBtn"
            @click="superDelBatchs()"
          >高级删除
          </el-button>
        </template>
        <template slot="menuRight">
          <el-button
            @click="handleDownloadClickDeliveryTemp"
            type="text"
            size="small"
          >模板下载</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="scope.row.isSend=='0'"
            @click.stop="rowEdit(scope.row,scope.index)">编 辑
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
  import baseCrud from '@/mixins/base-crud'
  import { addObj, sendBatch, ImportDeliveryTemp, templateDeliveryTemp,delBatch } from '@/api/subway/swstdeliveryplantemp'
  import { startDate, endDate } from "@/const/crud/subway/swstdeliveryplantemp";
  import { handleExcelResponse } from "@/util/util";
  export default {
    name: 'StPlanDeliveryTemp',
    mixins: [baseCrud('subway/swstdeliveryplantemp')],
    data() {
      return {
        // params: {
        //   planType: 3 // 配送计划
        // },
        searchForm: {
          planStartTime: startDate,
          planEndTime: endDate,
          isSend: '0'
        }
      }
    },
    computed: {
      permissionList() {
        return {
	    delBtn: this.vaildData(this.permissions.st_plan_delivery_tenp_super_delete, false)
        }
      }
    },
    methods: {
      //删除操作
      delBatchs: function () {
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据！");
          return false;
        }
        let ids = [];
        for (const table of this.$refs.crud.tableSelect) {
          if (table.isSend == '1') {
            this.$message.error("请检查数据，只能删除未下发的数据！");
            return false;
          }
          ids.push(table.id);
        }
        this.$confirm("确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delBatch(ids)
            .then((data) => {
              this.$message.success("删除成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {
              loading();
            });
        });
      },
       //高级删除操作
      superDelBatchs: function () {
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据！");
          return false;
        }
        let ids = [];
        for (const table of this.$refs.crud.tableSelect) {         
          ids.push(table.id);
        }
        this.$confirm("确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delBatch(ids)
            .then((data) => {
              this.$message.success("删除成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {
              loading();
            });
        });
      },
      rowSave: function (row, done, loading) {
        //row.planType = 3
        let startDate = Date.parse(row.planStartTime)
        let endDate = Date.parse(row.planEndTime)
        if(startDate > endDate){
          this.$message.warning('计划开始时间不能大于计划结束时间')
          loading()
          return
        }
	if (JSON.stringify(row.filePath) === '{}' || row.filePath==null  || row.filePath==""){
	    row.filePath="";	    
	}	
        addObj(row).then(data => {
          this.$message.success('添加成功')
          done()
          this.getList(this.page)
        }).catch(() => {
          loading()
        })
      },
      beforeOpen(done, type){
        if(type == 'add'){
          setTimeout(()=>{
            let today = dateFormat(new Date()).substr(0, 10) + ' 00:00:00'
            this.form.planStartTime = today
            this.form.planEndTime = today
          },0)
        }
        done()
      },
      handleSendBatch(){
        let tableSelect = this.$refs.crud.tableSelect
        if(tableSelect.length == 0){
          this.$message.info('请选择要下发的记录')
          return false
        }
        let ids = tableSelect.map(row => row.id)
        sendBatch(ids).then(response => {
          this.$message.success('下发成功')
          this.getList(this.page)
        })
      },
      async handleDownloadClickDeliveryTemp () {//车票临时计划配送信息导出模板
        this.uploading = true;
        try {
          const res = await templateDeliveryTemp();
          handleExcelResponse(res);
        } finally {
          this.uploading = false;
        }
      },
      handleImportDeliveryTemp(item) {//车票临时计划配送和回收导入
        this.$message.info("正在导入，请耐心等待！");
        let file = item.file;
        let param = new FormData();
        param.append("file", file);
        ImportDeliveryTemp(param).then(() => {
          this.$notify({
            title: "成功",
            message: "导入成功",
            type: "success",
            duration: 2000
          });
          this.getList(this.page);
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
</style>
